package com.lepu.app.fun.grow.adapter;

import java.util.ArrayList;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.app.utils.LoginUtil;
import com.core.lib.application.BaseFragmentActivity;
import com.core.lib.utils.main.DateUtilBase;
import com.core.lib.utils.main.UtilityBase;
import com.core.lib.utils.photo.UniversalImageLoadTool;
import com.core.lib.widget.CircularImageView;
import com.eyzhs.app.R;
import com.lepu.app.fun.grow.bean.HotPhoto;
import com.lepu.app.usercenter.UserOtherInfoActivity;

public class HotPhotoAdapter extends BaseAdapter {

	private LayoutInflater mLayoutInflater = null;
	private Context mContext = null;
	private ArrayList<HotPhoto> mArrayList = null;
	private int mWidth = 0;

	public HotPhotoAdapter(Context context, ArrayList<HotPhoto> arrayList) {
		mContext = context;
		mLayoutInflater = LayoutInflater.from(context);
		mArrayList = arrayList;
		mWidth = UtilityBase.getWindowWidthHeight((Activity) mContext)[0];
	}

	@Override
	public int getCount() {
		int size = 0;
		if (mArrayList != null && mArrayList.size() > 0) {
			size = mArrayList.size();
		}
		return size;
	}

	@Override
	public Object getItem(int position) {
		return position;
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@SuppressLint("InflateParams")
	@Override
	public View getView(int position, View convertView, ViewGroup parent) {

		final ViewHolder holder;

		if (convertView == null) {
			convertView = mLayoutInflater.inflate(
					R.layout.grow_hot_photo_listitem, null);

			holder = new ViewHolder();

			LinearLayout mainItemLayout = (LinearLayout) convertView
					.findViewById(R.id.mainItemLayout);
			ImageView albumCoverImageView = (ImageView) convertView
					.findViewById(R.id.albumCoverImageView);
			CircularImageView accountIconImageView = (CircularImageView) convertView
					.findViewById(R.id.accountIconImageView);
			TextView titleTextView = (TextView) convertView
					.findViewById(R.id.titleTextView);
			TextView timeTextView = (TextView) convertView
					.findViewById(R.id.timeTextView);
			TextView likeCountTextView = (TextView) convertView
					.findViewById(R.id.likeCountTextView);
			TextView subTitleTextView = (TextView) convertView
					.findViewById(R.id.subTitleTextView);

			ImageView likeImageView = (ImageView) convertView
					.findViewById(R.id.likeImageView);

			holder.mainItemLayout = mainItemLayout;
			holder.accountIconImageView = accountIconImageView;
			holder.titleTextView = titleTextView;
			holder.timeTextView = timeTextView;
			holder.likeCountTextView = likeCountTextView;
			holder.subTitleTextView = subTitleTextView;
			holder.albumCoverImageView = albumCoverImageView;
			holder.likeImageView = likeImageView;

			convertView.setTag(holder);
		} else {
			holder = (ViewHolder) convertView.getTag();
		}

		holder.mainItemLayout.setTag(position);

		final HotPhoto item = mArrayList.get(position);

		// holder.accountIconImageView.setImageBitmap(ImageUtilBase
		// .getBitmapByResId(mContext, R.drawable.default_icon_small));
		//
		// if (!TextUtils.isEmpty(item.Avatar)) {
		// String fileName = EncodeMD5.getMd5(item.Avatar);
		// String filePath = SdLocal.getCacheImagePath(mContext, fileName);
		// String imageUrl = item.Avatar;
		//
		// BitmapManager.getInstance().loadBitmap(imageUrl, filePath,
		// holder.accountIconImageView, true);
		// }
//		UniversalImageLoadTool.disPlay(item.Avatar,
//				holder.accountIconImageView, R.drawable.app_ic_default_avatar);
		LoginUtil.loadSelfUserImage(mContext, holder.accountIconImageView, item.Avatar);

		holder.titleTextView.setText(item.NickName);
		holder.timeTextView
				.setText(DateUtilBase.friendly_time(item.CreateTime));
		int FavourCount = UtilityBase.parseInt(item.FavourCount);
		holder.likeCountTextView.setText(String.format("%d人喜欢", FavourCount));
		holder.subTitleTextView.setText(item.AlbumDescription);

		LayoutParams params = holder.albumCoverImageView.getLayoutParams();
		params.width = mWidth;
		params.height = mWidth * 9 / 16;
		holder.albumCoverImageView.setLayoutParams(params);

		// album cover
		// if (!TextUtils.isEmpty(item.AlbumCover)) {
		// //String fileName = EncodeMD5.getMd5(item.AlbumCover);
		// //String filePath = SdLocal.getCacheImagePath(mContext, fileName);
		// String imageUrl = item.AlbumCover;
		//
		// // BitmapManager.getInstance().loadBitmap(imageUrl, filePath,
		// // holder.albumCoverImageView, true);
		// UniversalImageLoadTool.disPlay(imageUrl, holder.albumCoverImageView,
		// R.drawable.album_cover1);
		// }
		UniversalImageLoadTool.disPlay(item.AlbumCover,
				holder.albumCoverImageView, R.drawable.album_cover_default);

//		if (!TextUtils.isEmpty(item.HasFavour) && item.HasFavour.equals("0")) {
//			// 未赞
//			holder.likeImageView.setSelected(false);
//		} else {
//			holder.likeImageView.setSelected(true);
//		}
		if (FavourCount == 0) {
			// 未赞
			holder.likeImageView.setSelected(false);
		} else {
			holder.likeImageView.setSelected(true);
		}

		holder.accountIconImageView.setOnClickListener(new View.OnClickListener() {

			@Override
			public void onClick(View v) {
				LoginUtil.jumpUserInfo((BaseFragmentActivity)mContext, item.UserID);
			}
		});
		
		return convertView;
	}

	private static class ViewHolder {
		LinearLayout mainItemLayout;
		ImageView albumCoverImageView;
		CircularImageView accountIconImageView;
		TextView titleTextView;
		TextView timeTextView;
		TextView likeCountTextView;
		TextView subTitleTextView;
		ImageView likeImageView;
	}

}
